#!/usr/bin/env php
<?php
//院校详情采集爬虫
//php yikao_college_detail_spider.php start
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/bootstrap/database.php';
require_once __DIR__.'/bootstrap/arguments.php';

use App\Workers\YikaoCollegeDetailSpiderWorker;
use Workerman\Worker;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// 解析命令行参数
$args = parseArguments($argv);
$options = $args['options'];
$limit = $args['limit'] ?? 0;

$baseUrl = dirname(__DIR__).'/college';

// 创建Worker
$worker = new Worker();
$worker->name = 'YikaoCollegeDetailSpiderWorker';
$worker->count = 1;
$worker->reloadable = true;
$worker->user = '';
// 注释掉stdout重定向以在控制台显示输出
// $worker::$stdoutFile = $baseUrl . '/storage/tmp/yikao-college-detail-worker.stdout.log';
$worker::$pidFile = $baseUrl . '/storage/tmp/yikao-college-detail-worker.pid';
$worker::$logFile = $baseUrl . '/storage/tmp/yikao-college-detail-worker.log';

// 确保logs目录存在
$logDir = dirname(__DIR__).'/college/storage/logs';
if (!is_dir($logDir)) {
    mkdir($logDir, 0755, true);
}

// 直接输出调试信息
echo "========== 院校详情采集系统启动 ==========\n";
echo "当前时间: ".date('Y-m-d H:i:s')."\n";
echo "工作目录: ".getcwd()."\n";
echo "日志目录: {$logDir}\n";

// 初始化日志服务 - 同时输出到文件和控制台
try {
    $logger = new Logger('院校详情采集Worker');
    $logger->pushHandler(new StreamHandler($logDir.'/yikao-college-detail-worker.log', Logger::DEBUG));
    $logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
    echo "日志系统初始化成功\n";
    
    // 初始化Worker
    echo "正在初始化Worker...\n";
    YikaoCollegeDetailSpiderWorker::run($worker, $limit);
    echo "Worker初始化完成\n";
} catch (\Exception $e) {
    echo "初始化失败: ".$e->getMessage()."\n";
    echo "堆栈跟踪:\n".$e->getTraceAsString()."\n";
    exit(1);
}

Worker::runAll();
